import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xvideos/assets_tool/app_colors.dart';
import 'package:xvideos/extension/date_string_extension.dart';
import 'package:xvideos/hj_model/mine/ExchangeRecordModel.dart';
import 'package:xvideos/hj_page/mine_business/mine_setting/mine_code_logic.dart';
import 'package:xvideos/hj_utils/date_time_util.dart';
import 'package:xvideos/hj_utils/widget_utils.dart';
import 'package:xvideos/tools_base/loading/l_error_widget.dart';
import 'package:xvideos/tools_base/loading/loading_center_widget.dart';
import 'package:xvideos/tools_base/refresh/pull_refresh.dart';

//邀请码和兑换码
class MineExchangeCodePage extends StatefulWidget {
  final int type; //0-填写邀请码 1-填写兑换码
  const MineExchangeCodePage({super.key, this.type = 0});

  @override
  State<MineExchangeCodePage> createState() => _MineExchangeCodePageState();
}

class _MineExchangeCodePageState extends State<MineExchangeCodePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(widget.type == 0 ? '邀请码' : '领取兑换')),
      body: GetBuilder<MineExchangeCodeLogic>(
        init: MineExchangeCodeLogic(type: widget.type),
        builder: (_) => Padding(
            padding: EdgeInsets.symmetric(vertical: 28, horizontal: 16),
            child: Column(
              children: [
                Expanded(
                  child: pullYsRefresh(
                    onInit: (controller) => _.refreshController = controller,
                    onRefresh: (controller) => _.loadData(),
                    onLoading: (controller) => _.loadMoreData(),
                    child: CustomScrollView(
                      keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
                      slivers: <Widget>[
                        SliverToBoxAdapter(
                          child: _buildContent(_),
                        ),
                        if (widget.type == 1) ...[
                          _buildHistoryTable(_),
                        ]
                      ],
                    ),
                  ),
                ),
                InkWell(
                  onTap: () => _.onExChangeCode(),
                  child: Container(
                    height: 44,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.all(Radius.circular(3)),
                      color: AppColors.actionRed,
                    ),
                    child: Center(
                      child: Text(
                        "立即兑换",
                        style: const TextStyle(
                          color: Colors.white,
                          fontWeight: FontWeight.w600,
                          fontSize: 16,
                        ),
                      ),
                    ),
                  ),
                ),
              ],
            )),
      ),
    );
  }

  Widget _buildHistoryTable(MineExchangeCodeLogic logic) {
    if (logic.isLoadingHistoryData) {
      return SliverToBoxAdapter(child: LoadingCenterWidget());
    } else if (logic.groupList.isEmpty) {
      return SliverToBoxAdapter(
        child: CErrorWidget(errorMsg: "暂无兑换记录"),
      );
    } else {
      return SliverList.separated(
        itemBuilder: (context, index) {
          return _buildListItem(logic.groupList[index]);
        },
        itemCount: logic.groupList.length,
        separatorBuilder: (context, index) {
          return Divider(
            height: 1,
            color: Colors.black87.withOpacity(0.1),
          );
        },
      );
    }
  }

  _buildContent(MineExchangeCodeLogic _) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          "输入兑换码",
          style: TextStyle(
            color: Colors.white.withOpacity(.9),
            fontWeight: FontWeight.w500,
            fontSize: 30,
          ),
        ),
        12.sizeBoxH,
        Text(
          "每个兑换码只能输入一次",
          style: TextStyle(
            color: Color(0xff525252),
            fontWeight: FontWeight.w500,
            fontSize: 16,
          ),
        ),
        56.sizeBoxH,
        Container(
            height: 42,
            child: Row(
              children: [
                Text(
                  "兑换码",
                  style: TextStyle(
                    color: Colors.white.withOpacity(.9),
                    fontWeight: FontWeight.w500,
                    fontSize: 14,
                  ),
                ),
                16.sizeBoxW,
                Expanded(
                  child: TextField(
                    keyboardType: TextInputType.text,
                    autofocus: true,
                    autocorrect: true,
                    cursorColor: Colors.white,
                    textAlign: TextAlign.left,
                    controller: _.controller,
                    style: TextStyle(color: Colors.white, fontSize: 14),
                    decoration: InputDecoration(
                      hintText: '请输入${widget.type == 0 ? '邀请码' : '兑换码'}（字母大写）',
                      hintStyle: TextStyle(color: Color(0xff434c55)),
                      border: InputBorder.none,
                    ),
                  ),
                )
              ],
            )),
        12.sizeBoxH,
        1.line,
        18.sizeBoxH,
        Text(
          "官方社群领取更多福利",
          style: TextStyle(
            color: Color(0xff525252),
            fontWeight: FontWeight.w500,
            fontSize: 12,
          ),
        ),
        36.sizeBoxH,
        Text(
          "兑换记录",
          style: TextStyle(
            color: Colors.white.withOpacity(.9),
            fontWeight: FontWeight.w600,
            fontSize: 18,
          ),
        ),
        12.sizeBoxH,
        Row(
          children: [
            Expanded(child: _buildSectionItem('兑换码')),
            5.sizeBoxW,
            Expanded(child: _buildSectionItem('兑换类型')),
            5.sizeBoxW,
            Expanded(child: _buildSectionItem('兑换时间')),
          ],
        ),
      ],
    );
  }

  _buildSectionItem(String title) {
    return Container(
      alignment: Alignment.center,
      height: 42,
      decoration: BoxDecoration(
        color: Colors.white.withOpacity(.05),
      ),
      child: Text(
        title,
        style: TextStyle(
          color: Colors.white.withOpacity(.9),
          fontWeight: FontWeight.w400,
          fontSize: 14,
        ),
      ),
    );
  }

  Widget _buildListItem(ExchangeRecordModel item) {
    return Container(
      height: 44,
      child: Row(
        children: [
          Flexible(
            child: Container(
              height: 44,
              alignment: Alignment.center,
              child: Text(
                item.code ?? '',
                style: TextStyle(color: Colors.white.withOpacity(.55), fontSize: 14),
              ),
            ),
          ),
          Flexible(
            child: Container(
              height: 44,
              alignment: Alignment.center,
              child: Text(
                item.desc ?? '',
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                style: TextStyle(color: Colors.white.withOpacity(.55), fontSize: 14),
              ),
            ),
          ),
          Flexible(
            child: Container(
              height: 44,
              alignment: Alignment.center,
              child: Text(
                '${item.createdAt.utcTimeSToYMDString(gap: '.')}',
                style: TextStyle(color: Colors.white.withOpacity(.55), fontSize: 14),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
